package com.zzyl.nursing.controller;

import java.util.List;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DevicePropertiesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备表Controller
 *
 * @author ruoyi
 * @date 2025-06-08
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备表相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备表列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备表列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = " 从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = " 查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allPriduct());
    }

    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation(value = " 注册设备")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 查询设备详细数据
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = " 查询设备详细数据")
    public AjaxResult allProduct(@PathVariable String iotId) {
        DeviceDetailVo deviceVo = deviceService.getDeviceData(iotId);
        return success(deviceVo);
    }

    /**
     * 修改设备
     */
    @PutMapping()
    @ApiOperation(value = " 修改设备")
    public AjaxResult updateIotDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateIotDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = " 删除设备")
    public AjaxResult deleteIotDevice(@PathVariable String iotId) {
        deviceService.deleteIotDevice(iotId);
        return success();
    }

    /**
     * 查看设备上报的数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = " 查看设备上报的数据")
    public R<List<DevicePropertiesVo>> queryServiceProperties(@PathVariable String iotId) {
        List<DevicePropertiesVo> deviceData = deviceService.queryServiceProperties(iotId);
        return R.ok(deviceData);
    }

    @GetMapping("/queryProduct/{productKey}")
    public R<List<ServiceCapability>> queryProduct(@PathVariable String productKey) {
        List<ServiceCapability> serviceCapabilities = deviceService.queryProduct(productKey);
        return R.ok(serviceCapabilities);
    }
}
